package com.ychen.core.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * 监视器，spring启动成功之后打印
 *
 * @author chenyuliao
 * @version 1.0
 * @description
 * @date 2020/12/29 22:54
 */
@Component
public class MyEventListener implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {

    ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 打印容器中出事Bean的数量
        System.out.println("监听器获得容器中初始化Bean数量：" + event.getApplicationContext().getBeanDefinitionCount());
        NotifyEvent notifyEvent = new NotifyEvent("object", "abc@qq.com", "This is the content");
        applicationContext.publishEvent(notifyEvent);
    }


}
